#注册指纹代码
#按两次指纹登录一个模板存于flash指纹库
#执行PS_GetImage和PS_Genchar得到特征文件存到bufferID=1
#执行PS_GetImage和PS_Genchar得到特征文件存到bufferID=2
#执行regModel生成模版
#执行StoreChar将缓冲区的文件存储到flash指纹库
import time
import serial
import string
import binascii


def getImage():
    #获取指纹图像
    print("请按下手指")
    while True:
        s.write(GetImage)
        time.sleep(1)
        number=s.inWaiting()
        read= str(binascii.b2a_hex(s.read(number)))[2:-1]
        if read[19]=='0':
            print("指纹收集成功，请放开手指")
            break
#未检测到指纹信息


s=serial.Serial('/dev/ttyUSB0',115200)
GetImage = [0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X03,0X01,0X00,0X05]
GenChar_1= [0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X04,0X02,0X01,0X00,0X08]
GenChar_2= [0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X04,0X02,0X02,0X00,0X09]
RegModel=  [0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X03,0X05,0X00,0X09]
#最后的校验和要修改
StoreChar= [0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X06,0X06,0X01,0X00,0X07,0X00,0X15]

#最后的校验和要修改
PS_LoadChar=[0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X06,0X07,0X01,0X00,0X07,0X00,0X16]
PS_UpChar=  [0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X04,0X08,0X01,0X00,0X0E]

#获取指纹图像
getImage()

s.write(GenChar_1)
time.sleep(1)
number=s.inWaiting()
print(number)
read= str(binascii.b2a_hex(s.read(number)))[2:-1]
print(read)
if read[19]=='0':
    print("指纹存入缓冲区成功")
else:
    print("指纹存入缓冲区失败")
    
#获取指纹图像
getImage()

s.write(GenChar_2)
time.sleep(1)
number=s.inWaiting()
read= str(binascii.b2a_hex(s.read(number)))[2:-1]
print(read)
if read[19]=='0':
    print("指纹存入缓冲区成功")
else:
    print("指纹存入缓冲区失败")

s.write(RegModel)
time.sleep(1)
number=s.inWaiting()
read= str(binascii.b2a_hex(s.read(number)))[2:-1]
if read[19]=='0':
    print("指纹存为模版成功")
else:
    print("指纹存为模版失败")

s.write(StoreChar)
time.sleep(1)
number=s.inWaiting()
read= str(binascii.b2a_hex(s.read(number)))[2:-1]
print(read)
if read[19]=='0':
    print("指纹存到flash成功")
else:
    print("指纹存到flash失败")

#注册指纹到指纹模块完成


#现在开始上传指纹数据
print("PS_LoadChar")
s.write(PS_LoadChar)
time.sleep(1)
number=s.inWaiting()
#print(s.read(number))
read= str(binascii.b2a_hex(s.read(number)))[2:-1]
#read=s.read(number)
print("从flash中读取指定页码的指纹")
print (read)
if read[19]=='0':
    print("指纹读取成功")
else:
    print("指纹读取失败")
s.write(PS_UpChar)
time.sleep(1)
number=s.inWaiting()
print(number)
#print(s.read(number))
read= str(binascii.b2a_hex(s.read(number)))[2:-1]
print(read)

if read[19]=='0':
    print("指纹上传，接收后续数据包")
else:
    print("指纹上传失败")

s.write(PS_UpChar)
time.sleep(1)
number=s.inWaiting()
f=open('fingerModel.FBI','wb')
s.read(12)
number1=s.inWaiting()
print(number1)
f.write(s.read(number-12))

f.close()
s.close()
print("---------------------------")
#save_to_file('fingerModel.txt', read[24:])

